//
//  PlayerVC.m
//  VideoPlayer
//
//  Created by 白璐 on 16/9/18.
//  Copyright © 2016年 baidu. All rights reserved.
//

#import "PlayerVC.h"
#import "PlayerViewModel.h"
#import "PlayerControlVC.h"

@interface PlayerVC ()

@property(nonatomic, strong) PlayerControlVC* controlVC;

@property(nonatomic, assign) CGRect originalFrame;

@end

@implementation PlayerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (void)addChildViewController:(UIViewController *)childController {
    [super addChildViewController:childController];
    
    if ([childController isKindOfClass:[PlayerControlVC class]]) {
        self.controlVC = (PlayerControlVC*)childController;
        self.controlVC.delegate = self.viewModel;
        self.viewModel.actions = self.controlVC;
        
        self.controlVC.isFullScreen = self.isFullScreen;
        self.originalFrame = self.view.frame;
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        appDelegate.fullScreenVideoIsPlaying = 0;
    }
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    [self.viewModel layout:self.view];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [self.viewModel start];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}

- (void)deviceOrientationChange:(NSNotification *)notification {
    UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
    //    NSLog(@"D:%ld,%ld",[[UIApplication sharedApplication] statusBarOrientation],[UIDevice currentDevice].orientation);
    //靠旋转,还没切换到全屏的时候
    //转到向下不修改
    if(orientation == UIDeviceOrientationPortraitUpsideDown) {
        return;
    }
    
    
    if (orientation != UIDeviceOrientationFaceUp && orientation != UIDeviceOrientationFaceDown) {
        if(!UIDeviceOrientationIsLandscape(orientation)) {
            self.controlVC.isLandScape = NO;
            [UIView animateWithDuration:0.3f animations:^{
                [self.view setTransform:CGAffineTransformIdentity];
                //                if (self.isLive) {
                //                    self.view.frame = CGRectMake(0, 0, kScreenWidth, floorf(kScreenWidth * 0.57));
                //                } else{
                //                    self.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
                //                }
                
                
                if (@available(iOS 11.0, *)) {
                    self.view.frame = CGRectMake(0, 44, kScreenWidth, floorf(kScreenWidth * 0.57));
                } else{
                    self.view.frame = CGRectMake(0, 20, kScreenWidth, floorf(kScreenWidth * 0.57));
                }
            }];
        } else {
            [UIView animateWithDuration:0.3f animations:^{
                self.controlVC.isLandScape = YES;
                [self.view setTransform:CGAffineTransformIdentity];
                self.view.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
            }];
        }
    }
}

//是否支持设备自动旋转
- (BOOL)shouldAutorotate {
    return YES;
}
//返回支持设备横屏
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}
//
////当由模态方式推出的时候  优先支持的方向
//- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
//    if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft)
//    {
//        return UIInterfaceOrientationLandscapeLeft;
//    }controlStop
//    else {
//        return UIInterfaceOrientationLandscapeRight;
//    }
//
//}

@end
